<!--
 * @Description: 
 * @Author: wind-lc
 * @version: 1.0
 * @Date: 2022-11-28 09:43:58
 * @LastEditTime: 2022-12-01 11:17:17
 * @FilePath: \cscec-pms-admin\src\views\materials\procurement\apply\add\index.vue
-->
<template>
  <div class="about-master-container">
    <base-breadcrumb :breadcrumb="!isAudit">
      <b-steps
        :current="stepVal"
        direction="vertical"
        @change.self="stepChange"
      >
        <b-step title="基本信息">
          <a-card
            v-show="activeKey[0]"
            slot="description"
            v-loading="loading"
            :bordered="false"
          >
            <a-form-model
              ref="ruleForm"
              :model="formData"
              :rules="rules"
            >
              <a-row>
                <a-col span="8">
                  <!-- 编辑时才显示-->
                  <a-form-model-item label="物资招标采购申请编码">
                    <BaseInput
                      v-model="formData.code"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="招标名称" prop="name">
                    <BaseInput v-model="formData.name" :disabled="state === 3" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="是否集采">
                    <a-select v-model="formData.collect" disabled>
                      <a-select-option :value="1">是</a-select-option>
                      <a-select-option :value="0">否</a-select-option>
                    </a-select>
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="是否为安全物资" prop="securityMaterial">
                    <a-select v-model="formData.securityMaterial" :disabled="state === 3">
                      <a-select-option :value="0">是</a-select-option>
                      <a-select-option :value="1">否</a-select-option>
                    </a-select>
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="编号"
                    prop="number"
                  >
                    <BaseInput
                      v-model="formData.number"
                      :max-length="20"
                      :disabled="state === 3"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="合计金额（小写）"
                    prop="totalPrice"
                  >
                    <BaseInput
                      v-model="formData.totalPrice"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="分公司名称"
                    prop="branchCompanyId"
                  >
                    <SelectSubCompany
                      v-model="formData.branchCompanyId"
                      placeholder="请选择"
                      :disabled="state === 3"
                      @selectChange="formData.branchCompanyName = $event.orgName"
                    />
                  </a-form-model-item>
                </a-col>
                <template v-if="formData.collect === 0">
                  <a-col span="8">
                    <a-form-model-item
                      label="项目名称"
                      prop="projectId"
                    >
                      <SelectProject
                        ref="selectProject"
                        v-model="formData.projectId"
                        placeholder="请选择"
                        :disabled="state === 3"
                        :form="formData"
                        key-value="branchCompanyId"
                      />
                    </a-form-model-item>
                  </a-col>
                </template>
                <a-col span="8">
                  <a-form-model-item
                    label="合计金额（大写）"
                    prop="totalPriceUpper"
                  >
                    <BaseInput
                      v-model="formData.totalPriceUpper"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="拟开标时间"
                    prop="bidOpenDate"
                  >
                    <a-date-picker
                      v-model="formData.bidOpenDate"
                      value-format="YYYY-MM-DD"
                      type="date"
                      :disabled="state === 3"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="创建人"
                    prop="creatorName"
                  >
                    <BaseInput
                      v-model="formData.creatorName"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="创建时间"
                    prop="createTime"
                  >
                    <BaseInput
                      v-model="formData.createTime"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
            </a-form-model>
            <div
              v-if="!isAudit"
              class="pub-next-btn"
            >
              <a-button
                v-if="state !== 3"
                type="primary"
                @click="temporaryStorage()"
              >
                保存草稿
              </a-button>
              <a-button
                type="primary"
                @click="temporaryStorage(true)"
              >
                下一步
              </a-button>
            </div>
          </a-card>
        </b-step>
        <b-step title="招标物资明细">
          <a-card
            v-if="activeKey[1]"
            slot="description"
            :bordered="false"
          >
            <materials
              :id="formData.id"
              ref="materials"
              :state="state"
              @getTotalPrice="getEditInfo"
            />
            <div
              v-if="!isAudit"
              class="pub-next-btn"
            >
              <a-button
                type="primary"
                @click="previous"
              >
                上一步
              </a-button>
              <a-button
                type="primary"
                @click="next"
              >
                下一步
              </a-button>
            </div>
          </a-card>
        </b-step>
        <b-step title="推荐供应商信息">
          <a-card
            v-if="activeKey[2]"
            slot="description"
            :bordered="false"
          >
            <supplier
              :id="formData.id"
              :state="state"
            />
            <div
              v-if="!isAudit"
              class="pub-next-btn"
            >
              <a-button
                type="primary"
                @click="previous"
              >
                上一步
              </a-button>
              <a-button
                type="primary"
                @click="next"
              >
                下一步
              </a-button>
            </div>
          </a-card>
        </b-step>
        <b-step title="相关附件">
          <a-card
            v-if="activeKey[3]"
            slot="description"
            :bordered="false"
          >
            <u-table
              :upload-list="addFile"
              :delete-url="fileDel"
              :is-show="state !== 3"
              :list-url="fileList"
              :business-id="formData.id"
              business-code="MM04"
            />
            <div
              v-if="!isAudit"
              class="pub-next-btn"
            >
              <a-button
                type="primary"
                @click="previous"
              >
                上一步
              </a-button>
            </div>
          </a-card>
        </b-step>
      </b-steps>
      <div
        v-if="!isAudit"
        class="pub-header-btn"
      >
        <a-button
          v-if="state !== 3"
          :loading="loading"
          type="primary"
          @click="submit"
        >
          提交
        </a-button>
        <BasePrintButton
          v-if="formData.id && state == 3"
          :before-print-fn="beforePrintFn"
          style="display: inline-block"
        />
        <a-button
          v-checkVisibleInDetail
          @click="$close"
        >
          关闭
        </a-button>
      </div>
    </base-breadcrumb>
  </div>
</template>

<script>
import materials from '../components/materials/materials'
import supplier from '../components/supplier/supplier'
import {
  applyAddBasic, applyEditBasic, applyListDetail, audit,
  addFile, fileList, fileDel, applyBasicDetail, print
} from '@/api/material/procurement/apply'
import { themeChoose } from '@/mixin/mixin'
import riskListApi from '@/api/legal/risk/list'
import SelectSubCompany from '@/components/Select/SelectSubCompany'
import SelectProject from '@/components/Select/SelectProject'

export default {
  name: 'a' + Date.now(),
  mixins: [themeChoose],
  components: {
    materials,
    supplier,
    SelectProject,
    SelectSubCompany
  },
  props: {
    isAudit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      beforePrintFn: async () => {
        let res = await print({ id: this.formData.id })
        return res.data.content
      },
      activeKey: [true, false, false, false, false, false, false],
      state: Number(this.$route.query.state),
      isSubmit: false,
      loading: false,
      row: this.$route.query.row,
      formData: {
        createTime: this.getNowTime(),
        code: null,
        name: '',
        collect: 1,
        branchCompanyName: '',
        branchCompanyCode: null,
        branchCompanyId: '',
        number: null,
        bidOpenDate: null,
        creatorName: JSON.parse(sessionStorage.getItem('ycloud-user_info')).name,
        projectId: null,
        projectCode: null,
        totalPriceUnit: '',
        totalPriceUpper: '',
        totalPrice: ''
      },
      stepVal: 0,
      isNext: false,
      rules: {
        number: { required: true, message: '请输入编号', trigger: 'blur' },
        name: { required: true, message: '请输入招标名称', trigger: 'blur' },
        bidOpenDate: { required: true, message: '请选择拟开标时间', trigger: 'change' },
        securityMaterial: { required: true, message: '请选择是否为安全物资', trigger: 'change' },
        branchCompanyId: { required: true, message: '请选择分公司名称', trigger: 'change' },
        projectId: { required: true, message: '请选择项目', trigger: 'change' }
      },
      addFile: addFile(),
      fileDel: fileDel(),
      fileList: fileList()
    }
  },
  watch: {
    isAudit: {
      immediate: true,
      handler(val) {
        if (val) {
          this.state = 3
        }
      }
    },
    stepVal(newVal, oldVal) {
      this.$set(this.activeKey, newVal, true)
    }
  },
  mounted() {
    if (this.state !== 1) {
      this.formData.id = this.$route.query.id
      this.getEditInfo()
      this.isSubmit = true
    }else {
      this.init()
    }
  },
  methods: {
    async init() {
      let res2 = await riskListApi.queryCurrentInfo()
      if (res2.code === this.SUCCESS_CODE) {
        this.formData.collect = res2.data === 'PD' ? 0 : 1
      }
    },
    previous: function() {
      this.isNext = true
      this.stepVal--
      if (this.stepVal < 0) {
        this.stepVal = 0
      }
    },

    projectChange: function(val) {
      let _this = this
      _this.project.map(function(item) {
        if (item.code === val) {
          _this.formData.projectCode = val
          _this.formData.projectName = item.name
        }
      })
    },
    next: function(val) {
      this.isNext = true
      this.stepVal++
      if (this.stepVal >= 3) {
        this.stepVal = 3
      }
    },
    stepChange(val) {
      if (!this.formData.id) {
        if (val) {
          this.$message.warning('切换步骤前请先填写基本信息')
          this.stepVal = 0
        }
      } else {
        if (!this.isNext) {
          this.$set(this.activeKey, val, !this.activeKey[val])
        }
      }
      this.isNext = false
    },
    getTotalPrice: function() {
      applyListDetail(this.formData.id).then(res => {
        if (res.code === 200) {
          this.formData = { ...this.formData, ...res.data }
          this.getEditInfo()
        }
      })
    },
    // 获取编辑回显数据
    async getEditInfo() {
      this.loading = true
      let res = await applyBasicDetail({ id: this.formData.id })
      if (res.code === 200) {
        this.formData = { ...this.formData, ...res.data }
        this.formData.collect = this.formData.collect ? 1 : 0
        // this.formData.totalPriceUnit = this.formData.totalPrice + this.formData.totalPriceUnit
      }
      this.loading = false
    },
    submit() {
      // this.save('approve');
      if (this.isSubmit) {
        this.$submitConfirm(async () => {
          this.loading = true
          let res = await audit({ baseId: this.formData.id, auditStatus: this.formData.auditStatus })
          this.loading = false
          if (res.code === this.SUCCESS_CODE) {
            this.$message.success('提交成功')
            this.$close()
          }
        })
      } else {
        this.$message.warning('提交之前请先暂存')
      }
    },
    async temporaryStorage(isNext) {
      if (this.state === 3) {
        this.next()
        return
      }
      this.$refs.ruleForm.validate(async (valid, object) => {
        if (valid) {
          this.loading = true
          this.formData.bidOpenDate = this.getNowTime(this.formData.bidOpenDate)
          if (this.state === 2 || this.formData.id !== undefined) {
            let res = await applyEditBasic(this.formData)
            if (res.code === 200) {
              this.$message.success('暂存成功')
              this.isSubmit = true
              if (isNext) {
                this.next()
              }
            }
          } else {
            let res = await applyAddBasic(this.formData)
            if (res.code === 200) {
              this.$message.success('暂存成功')
              this.isSubmit = true
              this.formData.id = res.data.id
              if (isNext) {
                this.next()
              }
              await this.getEditInfo()
            }
          }
          this.loading = false
        } else {
          this.$message.warning("请填写完所有必填项信息")
          return false
        }
      })
    }
  }
}
</script>
